package com.buss.customer.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.customer.model.CustomerClass;
import com.buss.customer.service.CustomerClassService;

@Controller
@RequestMapping("/admin/customerclass/")
public class CustomerClassController extends BaseController {

	@Autowired
	private CustomerClassService classService;
	
	@Override
	@RequestMapping("page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("customerclass/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@RequestMapping("{type}")
	public ModelAndView editPage(@PathVariable String type, Long id){
		ModelAndView view = new ModelAndView("customerclass/edit");
		CustomerClass customerClass = new CustomerClass();
		CustomerClass parentClass = null;
		if(EDIT_PAGE.equals(type)){
			customerClass = classService.loadClass(id);
			if(customerClass != null && customerClass.getParentId() != 0){
				parentClass = classService.loadClass(customerClass.getParentId());
			}
		}else{
			if(id != 0){
				parentClass = classService.loadClass(id);
			}
		}
		if(parentClass == null){
			parentClass = new CustomerClass();
			parentClass.setId(0L);
			parentClass.setName("往来单位类别信息");
		}
		view.addObject("parentClass", parentClass);
		view.addObject("customerClass", customerClass);
		return view;
	}
	
	/**
	 * 新增往来单位分类信息
	 * @param customerClass
	 * @param request
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public Map<String,Object> add(CustomerClass customerClass,HttpServletRequest request){
		customerClass.setCompanyId(getCompanyId(request));
		CustomerClass tmpClass = classService.saveClass(customerClass);
		Map<String, Object> map = SysStaticParam.resultMessage("新增往来单位分类信息成功!");
		map.put("customerClass", tmpClass);
		return map;
	}
	
	/**
	 * 修改往来单位分类信息
	 * @param customerClass
	 * @param request
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Map<String,Object> edit(CustomerClass customerClass,HttpServletRequest request){
		customerClass.setCompanyId(getCompanyId(request));
		classService.editClass(customerClass);
		return SysStaticParam.resultMessage("修改往来单位分类信息成功!");
	}
	
	@Override
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		classService.removeClass(ids);
		return SysStaticParam.resultMessage("删除往来单位分类信息成功!");
	}

	/**
	 * 根据父分类获取子分类信息列表
	 * @param parentId
	 * @param name
	 * @param request
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> list(Long parentId, String name,HttpServletRequest request){
		ResultList<CustomerClass> rs = classService.queryByParentId(getCompanyId(request), parentId, name);
		Map<String, Object> map = SysStaticParam.resultMessage("获取往来单位分类信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
	
	
}
